// packageI/knowledge/knowledgeWhole/knowledgeWhole.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    logo_url: "",
    last_page: "",
    finished: false,
    current_page: 1,

    plugin: 'message-base',//message-base商城知识库，project-template供应链知识库
    itemOption: {
      image: 'image_url',
      name: 'name',
      desc: 'desc'
    },
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&'
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    })
  },
  init() {
    this.data.list = [];
    this.data.current_page = 1;
    this.data.finished = false;

    let api = new Map([
      ["message-base", {
        index: 'plugin.message-base.frontend.base.index',
        params: ()=> {
          return {page: this.data.current_page}
        },
        callback:(res)=>{
          this.data.current_page += 1;
          let { data, logo, current_page, last_page } = res.data;
          this.setData({
            logo_url: logo,
            list: this.data.list.concat(data)
          });
          if (current_page >= last_page) this.data.finished = true;
        }}
      ],
      ["project-template", {
        index: 'plugin.project-template.frontend.knowledge.knowledgeBaseList',
        params: ()=> {
          return {page: this.data.current_page}
        },
        callback:(res)=>{
          this.data.current_page += 1;
          let { data, current_page, last_page } = res.data;
          if (current_page >= last_page) this.data.finished = true;
          this.setData({
            itemOption: {
              image: 'banner',
              name: 'title',
              desc: 'content'
            },
            list: this.data.list.concat(data)
          });
        }}
      ],
    ]);

    
    if(this.options.plu) {
      this.setData({
        plugin: this.options.plu
      })
    }
    this.data.apiOption = api.get(this.data.plugin);
    
  },
  getData() {
    if (this.data.finished ) return;
    let urlStr = app.getNetAddresss(this.data.apiOption.index);
    app._getNetWork({
      url: urlStr,
      data: this.data.apiOption.params(),
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          return;
        }
        this.data.apiOption.callback(res.data);
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let value = wx.getStorageSync("yz_uid");
    let mid = "";
    if (value) {
      mid = value;
    }
    return {
      path: '/packageI/knowledge/knowledgeList/knowledgeList?plu=' + this.options.plu + '&bid='+  this.options.bid +  '&mid=' + mid
    }
  }
})